package com.example.userserver.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.userserver.dao.ActivityDao;
import com.example.userserver.entity.ActivityInfoCollectEntity;
import com.example.userserver.entity.ActivityInfoCollectEntity;
import com.example.userserver.service.ActivityService;
import com.example.userserver.util.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Optional;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityInfoCollectEntity> implements ActivityService {

    @Override
    public IPage<ActivityInfoCollectEntity> getPage(Integer pageNum, Integer pageSize) {
        Page<ActivityInfoCollectEntity> activityEntityPage = new Page<>(pageNum, pageSize);
        QueryWrapper<ActivityInfoCollectEntity> activityEntityQueryWrapper = new QueryWrapper<ActivityInfoCollectEntity>()
//                .like("associates", )
//                .like("unit_name", )
                .eq("is_delete", 0)
                .orderByDesc("create_date");

        IPage<ActivityInfoCollectEntity> activityEntityIPage = this.baseMapper.selectPage((IPage<ActivityInfoCollectEntity>) activityEntityPage,
                activityEntityQueryWrapper);
        return activityEntityIPage;
    }

    @Override
    public Object addActivityInfo(ActivityInfoCollectEntity activityInfoCollectEntity) {
        Assert.isNull(activityInfoCollectEntity.getNeeds(), "用户需求不能为空！");
        Assert.isNull(activityInfoCollectEntity.getUnitName(), "单位/学校全称不能为空！");
        Assert.isNull(activityInfoCollectEntity.getAssociates(), "联系人不能为空！");
        Assert.isNull(activityInfoCollectEntity.getPhoneNumber(), "联系电话不能为空！");

        activityInfoCollectEntity.setCreateDate(String.valueOf(LocalDate.now()));
        activityInfoCollectEntity.setIsDelete("0");

        int insert = this.baseMapper.insert(activityInfoCollectEntity);
        return Optional.ofNullable(insert).isPresent();
    }

    @Override
    public Object updateInfo(ActivityInfoCollectEntity activityInfoCollectEntity) {
        Assert.isNull(activityInfoCollectEntity.getId(), "被删除信息id不能为空！");
        Assert.isNull(activityInfoCollectEntity.getNeeds(), "用户需求不能为空！");
        Assert.isNull(activityInfoCollectEntity.getUnitName(), "单位/学校全称不能为空！");
        Assert.isNull(activityInfoCollectEntity.getAssociates(), "联系人不能为空！");
        Assert.isNull(activityInfoCollectEntity.getPhoneNumber(), "联系电话不能为空！");

        activityInfoCollectEntity.setUpdateBy(ShiroUtils.getUserId().toString());
        activityInfoCollectEntity.setUpdateDate(String.valueOf(LocalDate.now()));

        int update = this.baseMapper.updateById(activityInfoCollectEntity);
        return Optional.ofNullable(update).isPresent();
    }

    @Override
    public Object deleteInfoById(String id) {
        Assert.isNull(id, "被删除信息id不能为空！");

        ActivityInfoCollectEntity activityInfoCollectEntity = new ActivityInfoCollectEntity();
        activityInfoCollectEntity.setId(id);
        activityInfoCollectEntity.setIsDelete("1");
        activityInfoCollectEntity.setUpdateDate(String.valueOf(LocalDate.now()));
        activityInfoCollectEntity.setUpdateBy(String.valueOf(ShiroUtils.getUserId()));

        int delete = this.baseMapper.updateById(activityInfoCollectEntity);
        return Optional.ofNullable(delete).isPresent();
    }

    @Override
    public ActivityInfoCollectEntity selectById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void exportInfo(HttpServletResponse response) {

    }


}
